#ifndef _INPUT_H_
#define _INPUT_H_

#include "SDL.h"
#include <map>
#include <set>
#include <vector>

class Input
{
public:
	enum
	{
		NOCOMMAND = -1,
		LEFT,
		RIGHT,
		UP,
		DOWN,
		AIMGUN,
		SHOOT,
		ENTER,
		EXIT,
		COMMANDCT
	};

	bool Open();
	bool Save();
	void Close();

	void Event(const SDL_Event & event);

	int CmdTime(int cmd)
	{
		return cmdtimes[cmd];
	}

	bool CmdOn(int cmd)
	{
		return cmdtimes[cmd] >= 0;
	}

	bool CmdEdge(int cmd)
	{
		return cmdtimes[cmd] == 0;
	}

	void CmdStart(int cmd)
	{
		cmdtimes[cmd] = 0;
		edgebits |= CmdBit(cmd);
		onbits |= CmdBit(cmd);
	}

	void CmdEnd(int cmd)
	{
		cmdtimes[cmd] = -1;
		edgebits &= ~CmdBit(cmd);
		onbits &= ~CmdBit(cmd);
	}

	int CmdBit(int cmd)
	{
		return 1 << cmd;
	}

	int EdgeBits()
	{
		return edgebits;
	}

	int OnBits()
	{
		return onbits;
	}

	void Update();

	enum BindType
	{
		NOTBINDING,
		KEYBINDING,
		JOYBINDING
	};
	enum BindState
	{
		BINDING = -1,
		BINDFAIL,
		BINDOK
	};
	void BindStart(int bind, int cmd)
	{
		binding = bind;
		bindcmd = cmd;
		bindstate = BINDING;
	}

	int Binding()
	{
		return binding;
	}
	int BindState()
	{
		return bindstate;
	}

	int KeyBind(SDLKey key, int cmd);
	int JoyBind(int jb, int cmd);

	const char* KeyStr(SDLKey k)
	{
		return SDL_GetKeyName(k);
	}

	const wchar_t* CmdWStr(int cmd);

	const char* CmdKeyStr(int cmd)
	{
		return KeyStr(cmdkeys[cmd]);
	}

	void KeyRepeat(bool rpt);

private:
	std::map<SDLKey, int> keycmds;
	std::vector<SDLKey> cmdkeys;
	std::set<SDLKey> reservedkeys;

	std::vector<SDL_Joystick*> joysticks;
	std::map<int, int> jbcmds;

	short binding;
	short bindcmd;
	int bindstate;
	
	std::vector<int> cmdtimes;
	int edgebits;
	int onbits;
};

extern Input Inp;

#endif
